﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infra.Entity;

namespace Infra.Common.DAO
{
   public class MenuDAL
    {


       public static void Salvar(SEGMENU menu)
       {
           using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
           {
               if (menu.CODMENU != 0)
               {
                   //Alterar o tipo de produto
                   var MN = dc.SEGMENUs.Single(m => m.CODMENU == menu.CODMENU);
                   MN.CODMENUPAI = menu.CODMENUPAI;
                   MN.STRMENU = menu.STRMENU;
                   MN.STRCLASSNAME = menu.STRCLASSNAME;
                   MN.STRMENUTYPE = menu.STRMENUTYPE;
                   MN.STRIMAGENAME = menu.STRIMAGENAME;
                   MN.STRPARAMDATA = menu.STRPARAMDATA;
                   MN.STRIDSECURITY = menu.STRIDSECURITY;
                   dc.SubmitChanges();
               }
               else
               {
                   //Salvar o tipo de produto
                   dc.SEGMENUs.InsertOnSubmit(menu);
                   dc.SubmitChanges();
               }
           }
       }


       public static void Excluir(int idMenu)
       {
           using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
           {
               var MN = dc.SEGMENUs.Single(m => m.CODMENU == idMenu);
               dc.SEGMENUs.DeleteOnSubmit(MN);
               dc.SubmitChanges();
           }
       }



       public static List<SEGMENU> Pesquisar(int codigo, String nome)
       {
           List<SEGMENU> ListaMenus = null;

           using (DBInfraEntityDataContext dc = new DBInfraEntityDataContext())
           {
               var MN = from M in dc.SEGMENUs
                              where ((codigo.Equals(0) || codigo.Equals(M.CODMENU)) &&
                                      (nome.Equals(string.Empty) || nome.Equals(M.STRMENU.Contains(nome))))
                              select M;
               ListaMenus = MN.ToList();
           }
           return ListaMenus;
       }





    }
}
